package com.wl2o2o.designpatterns.singleton;

/**
 * @Author <a href="https://github.com/wl2o2o">程序员CSGUIDER</a>
 * @From <a href="https://wl2o2o.github.io">CSGUIDER博客</a>
 * @CreateTime 2024/3/20
 */


public class HungrySingleton {
    private String name;
    private HungrySingleton(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    /**
     * 饿汉单例写法一
     */
    // private static HungrySingleton instance = null;
    //
    // static {
    //     instance = new HungrySingleton();
    // }
    //
    // // 私有构造方法，防止被实例化
    // private HungrySingleton() {
    // }
    //
    // public static HungrySingleton getInstance() {
    //     return instance;
    // }


    /**
     * 饿汉单例写法二
     */
    private static HungrySingleton instance = new HungrySingleton();

    private HungrySingleton(){}

    public static HungrySingleton getInstance() {
        return instance;
    }

    public static void main(String[] args) {

        HungrySingleton.getInstance().setName("CSGUIDER");
        System.out.println(HungrySingleton.getInstance().getName());

    }




}

